package com.example.userp.app.myapplication;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.userp.app.myapplication.InnerPrient.InnerPrint;
import com.example.userp.app.myapplication.InnerPrient.bean.DishesPrintBean;
import com.example.userp.app.myapplication.InnerPrient.bean.PreSettlementPrintBean;
import com.example.userp.app.myapplication.InnerPrient.bean.StatementBean;
import com.example.userp.app.myapplication.InnerPrient.utils.AidlUtil;
import com.sunmi.peripheral.printer.InnerPrinterException;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Set;

import butterknife.ButterKnife;
import butterknife.OnClick;


public class InnerPrintActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inner_print);
        ButterKnife.bind(this);
        try {
            AidlUtil.getInstance().connectPrinterService(getApplicationContext());
            AidlUtil.getInstance().initPrinter();
        } catch (InnerPrinterException e) {
            e.printStackTrace();
            Log.e("tag", "连接失败: " + e.toString());
        }
    }

    @OnClick({R.id.dy_tv, R.id.textView})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.dy_tv:
                ArrayList<DishesPrintBean> dishs = new ArrayList<>();
                dishs.add(new DishesPrintBean("红烧鱼-乌鱼", "30", "2", "60", "份", "甜一点，不要辣"));
                dishs.add(new DishesPrintBean("地瓜叶", "10", "1", "10", "份", null));
                dishs.add(new DishesPrintBean("佛跳墙", "380", "1", "380", "份", "闷久点"));
                dishs.add(new DishesPrintBean("白灼虾", "28", "1", "28", "份", "一定要活虾！！！！"));
                dishs.add(new DishesPrintBean("土豆焖排骨", "380", "1", "380", "份", ""));
                dishs.add(new DishesPrintBean("米饭", "2", "2", "4", "份", null));
                onOrderForm(new PreSettlementPrintBean("鱼腾阁", "七号桌", "5", "2019-02-18 09:03", "易大师", dishs, "365", "798", "2019-02-18 10:03"));
                break;
            case R.id.textView:
                ArrayList<DishesPrintBean> dishs1 = new ArrayList<>();
                dishs1.add(new DishesPrintBean("红烧鱼-乌鱼", "30", "2", "60", "份", "甜一点，不要辣"));
                dishs1.add(new DishesPrintBean("地瓜叶", "10", "1", "10", "份", null));
                dishs1.add(new DishesPrintBean("佛跳墙", "380", "1", "380", "份", "闷久点"));
                dishs1.add(new DishesPrintBean("白灼虾", "28", "1", "28", "份", "一定要活虾！！！！"));
                dishs1.add(new DishesPrintBean("土豆焖排骨", "380", "1", "380", "份", ""));
                dishs1.add(new DishesPrintBean("米饭", "2", "2", "4", "份", null));
                PreSettlementPrint(new StatementBean("鱼腾阁", "七号桌", "B12852525545", "5", "2019-02-18 09:03", "易大师", dishs1, "365", "798", "3", "50", "4", "15", "微信", "456", "456", "2019-02-18 10:03"));

                break;
        }
    }

    /**
     * 预订单
     */
    private void onOrderForm(PreSettlementPrintBean preSettlementPrintBean) {
        if (preSettlementPrintBean == null) {
            Toast.makeText(getApplicationContext(), "打印内容不可为空！", Toast.LENGTH_LONG).show();
            return;
        }

        AidlUtil.getInstance().printText(preSettlementPrintBean.getShopName(), 1, 30, true, false);
        AidlUtil.getInstance().printNLine(1);
        AidlUtil.getInstance().printText("预结单", 1, 18, true, false);
        AidlUtil.getInstance().printNLine(1);
        AidlUtil.getInstance().printText("-------------------------------------", 1, 20, true, false);

        AidlUtil.getInstance().TextSetting(0, 22, true, false);
        AidlUtil.getInstance().printLineTable(new String[]{"桌  号:", preSettlementPrintBean.getTableNumber()}, new int[]{1, 3}, new int[]{0, 0}, null);
        AidlUtil.getInstance().printLineTable(new String[]{"人  数:", preSettlementPrintBean.getPeopleNumber() + "人"}, new int[]{1, 3}, new int[]{0, 0}, null);
        AidlUtil.getInstance().printLineTable(new String[]{"时  间:", preSettlementPrintBean.getTime()}, new int[]{1, 3}, new int[]{0, 0}, null);
        AidlUtil.getInstance().printLineTable(new String[]{"服务员:", preSettlementPrintBean.getWaiter()}, new int[]{1, 3}, new int[]{0, 0}, null);
        AidlUtil.getInstance().printText("-------------------------------------", 1, 20, true, false);
        AidlUtil.getInstance().TextSetting(0, 20, true, false);
        AidlUtil.getInstance().printLineTable(new String[]{"品名", "单价", "数量/单位", "金额"}, new int[]{3, 3, 2, 2}, new int[]{0, 1, 1, 2}, null);
        if (null != preSettlementPrintBean.getDishList()) {
            for (DishesPrintBean dishesPrintBean : preSettlementPrintBean.getDishList()) {
                AidlUtil.getInstance().printLineTable(new String[]{dishesPrintBean.getDishesName(), dishesPrintBean.getUnitPrice(), dishesPrintBean.getQuantity() + "/" + dishesPrintBean.getUnit(), dishesPrintBean.getAmount()}, new int[]{3, 3, 2, 2}, new int[]{0, 1, 1, 2}, null);
                if (!TextUtils.isEmpty(dishesPrintBean.getRemarks())) {
                    AidlUtil.getInstance().printLineTable(new String[]{"备  注:", dishesPrintBean.getRemarks()}, new int[]{1, 3}, new int[]{0, 0}, null);
                }
            }
        }
        AidlUtil.getInstance().printText("-------------------------------------", 1, 20, true, false);
        AidlUtil.getInstance().printNLine(1);
        AidlUtil.getInstance().TextSetting(0, 20, true, false);
        AidlUtil.getInstance().printLineTable(new String[]{"做法加价:", preSettlementPrintBean.getPracticeMarkup() + "元"}, new int[]{1, 2}, new int[]{0, 0}, null);
        AidlUtil.getInstance().printLineTable(new String[]{"消费合计:", preSettlementPrintBean.getTotalConsumption() + "元"}, new int[]{1, 2}, new int[]{0, 0}, null);
        AidlUtil.getInstance().printText("-------------------------------------", 1, 20, true, false);
        AidlUtil.getInstance().printNLine(1);
        AidlUtil.getInstance().printText("欢迎光临，用餐愉快", 1, 20, true, false);
        AidlUtil.getInstance().printNLine(1);
        AidlUtil.getInstance().printText(preSettlementPrintBean.getPrintTime(), 1, 20, true, false);
        AidlUtil.getInstance().printNLine(1);
        AidlUtil.getInstance().printText("-------------------------------------", 1, 20, true, false);
        AidlUtil.getInstance().printText("------------------------------", 1, 20, true, false);
        AidlUtil.getInstance().printNLine(1);
        AidlUtil.getInstance().printNLine(3);
    }

    /**
     * 结帐单
     */
    private void PreSettlementPrint(StatementBean statementBean) {
        if (statementBean == null) {
            Toast.makeText(getApplicationContext(), "打印内容不可为空！", Toast.LENGTH_LONG).show();
            return;
        }

        AidlUtil.getInstance().printText(statementBean.getShopName(), 1, 30, true, false);
        AidlUtil.getInstance().printNLine(1);
        AidlUtil.getInstance().printText("结帐单", 1, 18, true, false);
        AidlUtil.getInstance().printNLine(1);
        AidlUtil.getInstance().printText("-------------------------------------", 1, 20, true, false);

        AidlUtil.getInstance().TextSetting(0, 22, true, false);
        AidlUtil.getInstance().printLineTable(new String[]{"桌  号:", statementBean.getTableNumber()}, new int[]{1, 3}, new int[]{0, 0}, null);
        AidlUtil.getInstance().printLineTable(new String[]{"单  号:", statementBean.getOrderNumber()}, new int[]{1, 3}, new int[]{0, 0}, null);
        AidlUtil.getInstance().printLineTable(new String[]{"人  数:", statementBean.getPeopleNumber() + "人"}, new int[]{1, 3}, new int[]{0, 0}, null);
        AidlUtil.getInstance().printLineTable(new String[]{"时  间:", statementBean.getTime()}, new int[]{1, 3}, new int[]{0, 0}, null);
        AidlUtil.getInstance().printLineTable(new String[]{"服务员:", statementBean.getWaiter()}, new int[]{1, 3}, new int[]{0, 0}, null);
        AidlUtil.getInstance().printText("-------------------------------------", 1, 20, true, false);
        AidlUtil.getInstance().TextSetting(0, 20, true, false);
        AidlUtil.getInstance().printLineTable(new String[]{"品名", "单价", "数量/单位", "金额"}, new int[]{3, 2, 3, 2}, new int[]{0, 1, 1, 2}, null);
        if (null != statementBean.getDishList()) {
            for (DishesPrintBean dishesPrintBean : statementBean.getDishList()) {
                AidlUtil.getInstance().printLineTable(new String[]{dishesPrintBean.getDishesName(), dishesPrintBean.getUnitPrice(), dishesPrintBean.getQuantity() + "/" + dishesPrintBean.getUnit(), dishesPrintBean.getAmount()}, new int[]{3, 2, 3, 2}, new int[]{0, 1, 1, 2}, null);
                if (!TextUtils.isEmpty(dishesPrintBean.getRemarks())) {
                    AidlUtil.getInstance().printText("备  注:" + dishesPrintBean.getRemarks(), 0, 20, true, false);
                    AidlUtil.getInstance().printNLine(1);
                }
            }
        }
        AidlUtil.getInstance().printText("-------------------------------------", 1, 20, true, false);
        AidlUtil.getInstance().printNLine(1);
        AidlUtil.getInstance().TextSetting(0, 20, true, false);
        AidlUtil.getInstance().printLineTable(new String[]{"做法加价:", statementBean.getPracticeMarkup() + "元", "消费合计:", statementBean.getTotalConsumption() + "元"}, new int[]{1, 1, 1, 1}, new int[]{0, 0, 0, 0}, null);
        HashMap<String, String> map = new HashMap<>();
        map.put("赠菜金额:", statementBean.getGiftAmount());
        map.put("退菜金额:", statementBean.getReturnAmount());
        dynamicLineTable(map, "元");
        HashMap<String, String> map1 = new HashMap<>();
        map1.put("折扣金额:", statementBean.getDiscountAmount());
        map1.put("优惠金额:", statementBean.getDiscountedPrice());
        dynamicLineTable(map1, "元");

        AidlUtil.getInstance().printLineTable(new String[]{"收款金额:", statementBean.getCollectionAmount() + "元"}, new int[]{1, 3}, new int[]{0, 0}, null);
        AidlUtil.getInstance().printLineTable(new String[]{"收款方式:", statementBean.getPaymentMethod()}, new int[]{1, 3}, new int[]{0, 0}, null);
        AidlUtil.getInstance().TextSetting(0, 24, true, false);
        AidlUtil.getInstance().printLineTable(new String[]{"实收金额:", statementBean.getPaidAmount() + "元"}, new int[]{1, 2}, new int[]{0, 0}, null);
        AidlUtil.getInstance().printText("-------------------------------------", 1, 20, true, false);
        AidlUtil.getInstance().printNLine(1);
        AidlUtil.getInstance().printText("欢迎光临，用餐愉快", 1, 24, true, false);
        AidlUtil.getInstance().printNLine(1);
        AidlUtil.getInstance().printText(statementBean.getPrintTime(), 1, 24, true, false);
        AidlUtil.getInstance().printNLine(1);
        AidlUtil.getInstance().printText("-------------------------------------", 1, 20, true, false);
        AidlUtil.getInstance().printNLine(1);
        AidlUtil.getInstance().printNLine(3);
    }

    private void dynamicLineTable(HashMap<String, String> hashMap, String unit) {
        ArrayList<String> text = new ArrayList<>();
        Set<String> keySet = hashMap.keySet();
        for (String key : keySet) {
            String value = hashMap.get(key);
            if (!TextUtils.isEmpty(value) && onStringToDouble(value) != 0) {
                text.add(key);
                text.add(value + unit);
            }
        }
        if (text.size() == 0) {
            return;
        }
        int[] colsWidthArr = new int[text.size()];
        int[] colsAlign = new int[text.size()];
        String[] textArry = new String[text.size()];
        for (int i = 0; i < text.size(); i++) {
            colsWidthArr[i] = 1;
            colsAlign[i] = 0;
            textArry[i]=text.get(i);
        }
        AidlUtil.getInstance().printLineTable( textArry, colsWidthArr, colsAlign, null);
    }

    /**
     * Sring转为 double
     */
    private double onStringToDouble(String s) {
        try {
            double aDouble = Double.valueOf(s);
            return aDouble;
        } catch (Exception e) {

        }
        return 0;
    }
}
